package kit.edu.lego.gruppe3.levels.fallingBridge;

import kit.edu.lego.gruppe3.ComponentsRepository;
import kit.edu.lego.gruppe3.LegoMain;
import kit.edu.lego.gruppe3.levels.LevelSelector;
import kit.edu.lego.gruppe3.levels.LevelSelector.Level;
import kit.edu.lego.gruppe3.sensors.GroundType;
import kit.edu.lego.gruppe3.sensors.Scanner;
import lejos.robotics.navigation.DifferentialPilot;
import lejos.robotics.subsumption.Behavior;

public class FallingBridgeEnd implements Behavior {

	@Override
	public void action() {
		DifferentialPilot pilot = ComponentsRepository.getInstance()
				.getRobotPilot();
		FallingBridgeSolver.setLevelCleared(true);

		LevelSelector.getInstance().setCurrentLevel(Level.line);
		Scanner.scanAt(0);
		pilot.steer(-20);
		while (pilot.isMoving())
			if (Scanner.getGroundTypeAt(0, true) == GroundType.Line)
				pilot.stop();
		LegoMain.startLine();
	}

	@Override
	public void suppress() {

	}

	@Override
	public boolean takeControl() {
		return LevelSelector.getInstance().isFallingBridge()
				&& ComponentsRepository.getInstance().getLightSensor()
						.getNormalizedLightValue() > 440;
	}

}
